package com.qingge.springboot.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qingge.springboot.common.Result;

import com.qingge.springboot.service.IShoppingCartService;
import com.qingge.springboot.entity.ShoppingCart;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 尹茂金
 * @since 2022-05-11
 */
@RestController
@RequestMapping("/shopping-cart")
public class ShoppingCartController {

    @Resource
    private IShoppingCartService shoppingCartService;

    // 新增或者更新
    @PostMapping
    public Result save(@RequestBody ShoppingCart shoppingCart) {
        shoppingCartService.saveOrUpdate(shoppingCart);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        shoppingCartService.removeById(id);
        return Result.success();
    }

    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        shoppingCartService.removeByIds(ids);
        return Result.success();
    }

    @GetMapping
    public Result findAll() {
        return Result.success(shoppingCartService.list());
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(shoppingCartService.getById(id));
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "") String name,
                           @RequestParam(defaultValue = "") String specification,
                           @RequestParam(defaultValue = "") String userId
    ) {
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        if (!"".equals(name)) {
            queryWrapper.like("merchandise_name", name);
        }
        if (!"".equals(specification)) {
            queryWrapper.like("specification", specification);
        }
        if (!"".equals(userId)) {
            queryWrapper.like("user_id", userId);
        }
        queryWrapper.orderByDesc("id");
        return Result.success(shoppingCartService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    @GetMapping("/pageZero")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "") String name,
                           @RequestParam(defaultValue = "") String specification
    ) {
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        if (!"".equals(name)) {
            queryWrapper.like("merchandise_name", name);
        }
        if (!"".equals(specification)) {
            queryWrapper.like("specification", specification);
        }
        queryWrapper.orderByDesc("id");
        return Result.success(shoppingCartService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

}

